package com.ja10.books;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;

public class RentalList {

	private int id;
	private String title;
	private String writer;
	private String rentalDate;
	private String returnPlanDate;
	private Bitmap pictureBmp;
	private byte[] pictureByte;
	private String isbn;
	
	// 画像フォルダのパスを設定
	final static String PATH = "http://192.168.3.190:8080/Books/book_img/";
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getRentalDate() {
		return rentalDate;
	}
	public void setRentalDate(String rentalDate) {
		this.rentalDate = rentalDate;
	}
	public String getReturnPlanDate() {
		return returnPlanDate;
	}
	public void setReturnPlanDate(String returnPlanDate) {
		this.returnPlanDate = returnPlanDate;
	}
	public Bitmap getPictureBmp() {
		return pictureBmp;
	}
	public void setPictureBmp(String picture, int size) throws IOException {
		// 画像のパスからビットマップの生成
		URL url = new URL(PATH + picture + ".jpg");
		InputStream input= url.openStream();
		Bitmap bm = BitmapFactory.decodeStream(input);
		// 画像のサイズ変更
		int w = bm.getWidth();
		int h = bm.getHeight();
		float scale = Math.min((float)size/w, (float)size/h);
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		
		this.pictureBmp = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
	}
	public void setPictureBmp(byte[] picture) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		this.pictureBmp = BitmapFactory.decodeByteArray(picture, 0, picture.length, options);
	}
	public byte[] getPictureByte() {
		return pictureByte;
	}
	public void setPictureByte(Bitmap picture) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		picture.compress(CompressFormat.JPEG, 100, os);

		this.pictureByte = os.toByteArray();
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

}